Page({
  data: {
    latitude: 34.753811,
    longitude: 113.677391,
    markers: [
      {
        iconPath: "../images/amap_start.png",
        id: 0,
        latitude: 34.753811,
        longitude: 113.677391,
        width: 35,
        height: 35
      },
      {
        iconPath: "../images/amap_end.png",
        id: 1,
        latitude: 34.756329,
        longitude: 113.677032,
        width: 35,
        height: 35
      },
    ],
    orderInfo:{},
    polyline: [{
      points: [{
        longitude: 113.677391,
        latitude: 34.753811
      }, 
      {
        longitude: 113.677032,
        latitude: 34.756329
      }],
      color: "#FF0000DD",
      width: 2,
      dottedLine: true
    }],
    points:[],
    carName:"",
    isUpThings:"不需要上货",
    peoples:"",
    phone:""
  },
  onReady: function (e) {
    this.mapCtx = wx.createMapContext('myMap');
    this.mapCtx.moveToLocation()
  },
  onLoad:function(options){
    var item = JSON.parse(decodeURIComponent(options.item))
    console.log(item)
    var sloation = item.slocation.split(',');
    var slat = sloation[0];
    var slon = sloation[1];

    var eloation = item.elocation.split(',');
    var elat = eloation[0];
    var elon = eloation[1];



    var isThings;
    if (item.isupdown == 1){
      isThings = "需上货"
    }else{
      isThings = "不需要上货"
    }
    this.setData({
      orderInfo: item,
      carName: item.carName,
      isUpThings: isThings,
      peoples: item.personcount,
      phone: item.knightPhone,
      polyline: [{
        points: [{
          longitude: slon,
          latitude: slat
        },
        {
          longitude: elon,
          latitude: elat
        }],
        color: "#FF0000DD",
        width: 2,
        dottedLine: true
      }],
      latitude: slat,
      longitude: slon,
      markers: [
        {
          iconPath: "../images/amap_start.png",
          id: 0,
          latitude: slat,
          longitude: slon,
          width: 35,
          height: 35
        },
        {
          iconPath: "../images/amap_end.png",
          id: 1,
          latitude: elat,
          longitude: elon,
          width: 35,
          height: 35
        },
      ],
      points:[
        {
          longitude: slon,
          latitude: slat
        },
        {
          longitude: elon,
          latitude: elat
        }
      ]
    })
  },
  getCenterLocation: function () {
    this.mapCtx.getCenterLocation({
      success: function (res) {
        console.log(res.longitude)
        console.log(res.latitude)
      }
    })
  },
  moveToLocation: function () {
    this.mapCtx.moveToLocation()
  },
  translateMarker: function () {
    this.mapCtx.translateMarker({
      markerId: 1,
      autoRotate: true,
      duration: 1000,
      destination: {
        latitude: 23.10229,
        longitude: 113.3345211,
      },
      animationEnd() {
        console.log('animation end')
      }
    })
  },
  includePoints: function () {
    this.mapCtx.includePoints({
      padding: [10],
      points: [{
        latitude: 23.10229,
        longitude: 113.3345211,
      }, {
        latitude: 23.00229,
        longitude: 113.3345211,
      }]
    })
  },
  cell_phone:function(e){
    var that = this;
    wx.makePhoneCall({
      phoneNumber: that.data.phone,
      success:function(e){
        console.log('拨打电话成功')
      },
      fail:function(e){
        console.log('拨打电话失败')
        console.log(e.errMsg)
      }
    })
  },
  cancel_click:function(e){
    const that = this;
    var orderInfo = that.data.orderInfo;
    const user = wx.getStorageSync("we_userInfo");
    var openid = user.user.openId;
    var carName = orderInfo.carName;
    var orderId = orderInfo.orderId;
    var agentId = orderInfo.agentId;
    var price = orderInfo.price;

    var data = JSON.stringify({
      openId: user.user.openId,
      carName: carName,
      orderId: orderId,
      agentId: agentId,
      price: price
    })
    console.log(JSON.parse(data));

    if (user) {
      wx.request({
        url: getApp().globalData.requestURL + '/api/order/cancel',
        data: JSON.stringify({
          openId: user.user.openId,
          carName: carName,
          orderId: orderId,
          agentId: agentId,
          price: price
        }),
        method: 'POST',
        header: {
          "content-type": "application/json"
        },
        success: function (res) {
          console.log(res)
          if (res.data.code == "SUCCESS"){
            wx.showModal({
              title: '提示',
              content: '您已成功取消订单',
              showCancel: false,
              success(res) {
                wx.navigateBack({
                  delta: 1
                })
              }
            })
          }else{
            wx.showModal({
              title: '提示',
              content: '取消订单失败',
              showCancel: false,
            })
          }
        },
        fail: function (err) {
          console.log(err.errMsg)
        }
      })
    } else {
      console.log(" 获取缓存失败")
    }
  }
})
